//
//  PBRentTypeController.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/6.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBRentTypeController.h"
#import "PBHomeCell.h"
#import "PBBestHomeModel.h"
#import "PBHomeDetailController.h"
#import "TMEmptyView.h"

static NSString * const kPBHomeCell = @"PBHomeCell";

@interface PBRentTypeController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) TMEmptyView *emptyView;
@end

@implementation PBRentTypeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
}
- (void)configureView {
    self.page = 1;
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeCell bundle:nil] forCellReuseIdentifier:kPBHomeCell];
    // 刷新
    [TMRefreshHelper headerRefresh:self.tableView withTarget:self action:@selector(headerRefresh)];
    [TMRefreshHelper footerRefresh:self.tableView withTarget:self action:@selector(footerRefresh)];
}
- (void)configureData {
    if (self.type == 5) {
        [self showHud];
        NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId,@"currentPage":@"1",@"pageSize":@"10"};
        [TMNetwork requestPostWithParam:param url:@"/house/excellent/query" callback:^(TMBaseReqModel *model) {
            [self hideHud];
            if (model.code == 200) {
                self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
                if (self.dataSource.count < 10) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                [self.tableView reloadData];
            }else {
                [self showHint:model.message];
            }
        }];
    }else {
        NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
        [param setValue:@"1" forKey:@"currentPage"];
        [param setValue:@"10" forKey:@"pageSize"];
        if (self.type == 1) {
            [param setObject:@"false" forKey:@"entireRent"];
        }else if (self.type == 2) {
            [param setObject:@"true" forKey:@"entireRent"];
        }else if (self.type == 3) {
            [param setObject:@"true" forKey:@"ownerRent"];
        }else if (self.type == 4) {
            [param setObject:@"true" forKey:@"apartment"];
        }
        [self showHud];
        [TMNetwork requestPostWithParam:param url:@"/house/query" callback:^(TMBaseReqModel *model) {
            [self hideHud];
            [self.tableView.mj_header endRefreshing];
            if (model.code == 200) {
                self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
                if (self.dataSource.count < 10) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                if (self.dataSource.count == 0) {
                    [self.tableView addSubview:self.emptyView];
                }else {
                    [self.emptyView removeFromSuperview];
                }
                [self.tableView reloadData];
            }else {
                [self showHint:model.message];
            }
        }];
    }
}

- (void)requestMoreData {
    if (self.type == 5) {
        NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId,@"pageSize":@"10",@"currentPage":[NSString stringWithFormat:@"%zd",self.page]};
        [TMNetwork requestPostWithParam:param url:@"/house/excellent/query" callback:^(TMBaseReqModel *model) {
            if (model.code == 200) {
                NSArray *array = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
                if (array.count < 10) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
            }else {
                [self showHint:model.message];
            }
        }];
    }else {
        NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
        [param setValue:[NSString stringWithFormat:@"%zd",self.page] forKey:@"currentPage"];
        [param setValue:@"10" forKey:@"pageSize"];
        if (self.type == 1) {
            [param setObject:@"false" forKey:@"entireRent"];
        }else if (self.type == 2) {
            [param setObject:@"true" forKey:@"entireRent"];
        }else if (self.type == 3) {
            [param setObject:@"true" forKey:@"ownerRent"];
        }else if (self.type == 4) {
            [param setObject:@"true" forKey:@"apartment"];
        }
        [TMNetwork requestPostWithParam:param url:@"/house/query" callback:^(TMBaseReqModel *model) {
            if (model.code == 200) {
                NSArray *array = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
                if (array.count < 10) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer endRefreshing];
                }
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
            }else {
                [self showHint:model.message];
            }
        }];
    }
}

- (void)headerRefresh {
    self.page = 1;
    [self configureData];
}

- (void)footerRefresh {
    self.page += 1;
    [self requestMoreData];
}


#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBHomeCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBBestHomeModel *model = self.dataSource[indexPath.row];
    PBHomeDetailController *vc = [[PBHomeDetailController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.houseId = [NSString stringWithFormat:@"%zd",model.ID];
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    if (self.type == 1) {
        self.title = @"合租";
    }else if (self.type == 2) {
        self.title = @"整租";
    }else if (self.type == 3) {
        self.title = @"业主直租";
    }else if (self.type == 4) {
        self.title = @"荣者公寓";
    }else if (self.type == 5) {
        self.title = @"推荐房源";
    }
}

- (TMEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [TMEmptyView TMEmptyView];
        _emptyView.frame = self.view.frame;
    }
    return _emptyView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
